// pages/login/accountLogin/forgot/forgot.js
const app = getApp()
const pws = require("../../../../utils/pws.js")
Page({
  data: {
    step:1,
    focus:1,
    confirmHold:true,
    val:[],
    step1:false,
    step2:false,
    step3: false,
    phone:'',
    code:'',
    pwd:'',
    time:'60',
    send:true,
    sendFlag:true,
    eyes:false,
    focusFlag:false
  },
  look: function (e) {
    this.setData({
      eyes: !this.data.eyes,
      focusFlag: true
    })
  },
  verifyPhone:function(e){ 
    if(e.detail.value.length == '11'){
      this.setData({
        step1:true,
        phone: e.detail.value
      })      
    }
  },
  verifyCode: function (e) {
    this.setData({
      val: e.detail.value
    })
    if (e.detail.value.length == '6') {
      this.setData({
        step2: true,
        code: e.detail.value
      })
    }
  },
  verifyPwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
    if (e.detail.value.length >= '8') {
      this.setData({
        step3: true
      })
    }
  },
  formSubmit:function(e){
    const x = e.target.dataset.num
    let str = `wxValidate${x}`
    const error = this[str].errorList[0]
    if (pws.pwStrength(this.data.pwd) == '1') {
      wx.showToast({
        title: '密码格式不符',
        image: '/images/register/cuowu@2x.png',
        duration: 2000
      })
      return
    }
    if (!this[str].checkForm(e)) {
      wx.showToast({
        title: this[str].errorList[0].msg,
        image: '/images/register/cuowu@2x.png',
        duration: 2000
      })
      return false
    }else{
      this.setData({
        sendFlag: true
      })
      switch (x) {
        case '':
          let param = {
            verCode:'',
            phone: this.data.phone
          }
          this.req('/forgotPsw',param)
          break;
        case "1":
          let param1 = {
            verCode: this.data.code,
            phone: this.data.phone
          }
          this.req('/forgotPsw', param1)        
          break;
        default:
          let param2 = {
            password: this.data.pwd,
            phone: this.data.phone
          }
          this.req('/updatePwd', param2)      
      }
    }
  },
  reSend: function(){
    let para = {
      verCode: '',
      phone: this.data.phone
    }
    this.setData({
      sendFlag: false
    })
    this.req('/forgotPsw', para)
  },
  req: function(url,param){
    wx.showLoading({
      title: '',
      mask: true,
      success:() => {
        wx.request({
          url: app.globalData.baseUrl + url,
          data: param,
          header: {
            'content-type': 'application/json'
          },
          method: 'POST',
          success: res => {
            // console.log(res.data)
            wx.hideLoading()
            if (res.data.code == '0') {
              //倒计时
              let step
              if (this.data.step < 3) {
                if(this.data.sendFlag){
                  step = ++this.data.step
                  this.setData({
                    step: step
                  })
                }
                // console.log(this.data.step)
                if(this.data.step == '2'){
                  this.setData({
                    time: '60',
                    send:true
                  })
                  let time1 = setInterval(() => {
                    if (this.data.time <= 0) {
                      clearInterval(time1)
                      this.setData({
                        send:false
                      })
                    } else {
                      this.setData({
                        time: --this.data.time
                      })
                    }
                  }, 1000)
                }
              } else {
                wx.showToast({
                  title: '密码找回成功',
                  icon: 'success',
                  duration: 1500,
                  success: (e) =>{
                    setTimeout(()=>{
                      wx.navigateBack({
                        delta: 1
                      })
                    },1500)
                    
                  }
                })
                
              }

            } else if (res.data.code == '1') {
              wx.showToast({
                title: res.data.data.Exception,
                image: '/images/register/cuowu@2x.png',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.data.message,
                image: '/images/register/cuowu@2x.png',
                duration: 2000
              })
            }
          },
        })
      }
    })
    
  },
  uncode: function (e) {
    wx.showModal({
      content: '请拨打客服电话：400-996-3626',
      confirmText: '拨打',
      confirmColor: '#2A8EFF',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '400-996-3626'
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  next:function(e){
    if(e.detail.value == ''){
      this.data.focus == 1 ? 1 : this.data.focus--
      this.setData({
        focus: this.data.focus
      })
    }else{
      this.data.focus == 6 ? 6 : this.data.focus++
      this.setData({
        focus: this.data.focus
      })
      if(this.data.focus == 6){

      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.wxValidate = app.wxValidate(
      {
        mobile: {
          required: true,
          tel: true,
        }, 
      }, {
        mobile: {
          required: '请填写手机号'
          // tel: 'dsfsdfsdf'
        },
      }
    )
    this.wxValidate1 = app.wxValidate1(
      {
        code: {
          required: true,
          minlength: 6
        }
      }, {
        code: {
          required: '请填写验证码',
          minlength: '验证码错误'
        }
      }
    ),
    this.wxValidate2 = app.wxValidate2(
      {
        pwd: {
          required: true,
          minlength: 8
        },
        pwdSet: {
          required: true,
          minlength: 8,
          equalTo: 'pwd'
        }
      }, {
        pwd: {
          required: '请填写新密码',
          minlength: '不少于8位'
        },
        pwdSet: {
          required: '请填写新密码',
          minlength: '不少于8位',
          equalTo: '密码不一致'
        }
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})